package com.xiaozhichao.javaapi.base.stream;

import org.junit.BeforeClass;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest {

    static List<Map<String,Object>> list = null;

    @BeforeClass
    public static void before(){
        Map<String,Object> map1=new HashMap<>();
        map1.put("id","1");
        map1.put("name","小张");
        map1.put("age","10");
        map1.put("sex","男");
        Map<String,Object> map2=new HashMap<>();
        map2.put("id","2");
        map2.put("name","小李");
        map2.put("age","15");
        map2.put("sex","女");
        Map<String,Object> map3=new HashMap<>();
        map3.put("id","3");
        map3.put("name","小王");
        map3.put("age","30");
        map3.put("sex","男");
        list=  Arrays.asList(map1,map2,map3);
    }

    /**
     * 过滤
     * 不会改变原集合
     */
    @Test
    public void filterTest(){
        System.out.println("filterTest before list " + list);
        List<Map<String,Object>> result = list.stream().filter(stu->
            stu.get("sex").equals("男")//lamda表达式中 ->右边返回boolean类型：true 则留下，false则剔除
        ).collect(Collectors.toList());
        System.out.println("filterTest after list " + list);
        System.out.println("filterTest after result " + result);
    }

    /**
     * Map是将list中的元素改变
     * 第一种返回原对象，会改变原集合
     * 第二种返回新对象，不会改变原集合
     */
    @Test
    public void mapTest(){
        System.out.println("mapTest before list " + list);
        List<Map<String,Object>> result = list.stream().map(
                stu->
                /*{
                    stu.put("score",80);
                    return stu;
                }*/
                {
                    Map<String,Object> map = new HashMap<>();
                    map.put("id",stu.get("id"));
                    map.put("name",stu.get("name"));
                    map.put("age",stu.get("age"));
                    map.put("sex",stu.get("sex"));
                    map.put("score",80);
                    return map;
                }
        ).collect(Collectors.toList());
        System.out.println("mapTest after list " + list);
        System.out.println("mapTest after result " + result);
    }

    @Test
    public void mapTest1(){
        System.out.println("mapTest1 before list " + list);
        List<String> result = list.stream().map(
                stu -> {return stu.get("name").toString();}
        ).collect(Collectors.toList());
        System.out.println("mapTest1 after list " + list);
        System.out.println("mapTest1 after result " + result);
    }

    /**
     * 将list中的元素拆分为多个list
     */
    @Test
    public void flatMapTest(){
        // 修改list name --> a-b
        list.stream().map(
                stu->
                {
                    stu.put("name",stu.get("name").toString().substring(0,1)+"-"+stu.get("name").toString().substring(1));
                    return stu;
                }
        ).collect(Collectors.toList());
        System.out.println("flatMapTest before list " + list);
        List<String> result = list.stream().flatMap(
                stu -> {return Arrays.stream(stu.get("name").toString().split("-"));}
        ).collect(Collectors.toList());
        System.out.println("flatMapTest after list " + list);
        System.out.println("flatMapTest after result " + result);
    }

}
